#include "ATMOS_NetworkConnectionInfo.h"

#include <fstream>

using namespace ATMOS;

NetworkConnectionInfo::NetworkConnectionInfo() :
serverPort( 0 ),
clientPort( 0 ),
connectionTimeOut( 0 ),
maxConnections( 0 ),
occasionalPing( false ),
shutdownTime( 200 ),
unreliableTimeout( 1000 )
{
}

NetworkConnectionInfo::NetworkConnectionInfo( const NetworkConnectionInfo &src ) :
	connectionPassword( src.connectionPassword ),
	serverIP( src.serverIP ),
	serverPort( src.serverPort ),
	clientPort( src.clientPort ),
	connectionTimeOut( src.connectionTimeOut ),
	maxConnections( src.maxConnections ),
	occasionalPing( src.occasionalPing ),
	shutdownTime( src.shutdownTime ),
	unreliableTimeout( src.unreliableTimeout )
{
}

NetworkConnectionInfo::~NetworkConnectionInfo()
{
}

void NetworkConnectionInfo::loadFromFile( const std::string &filename )
{
	std::ifstream file;
	file.open( filename.c_str(), std::ios::in );
	if( !file.is_open() )
	{
		ATMOS_THROW_RT("failed to open file: " << filename);
	}

	file	>> serverIP
			>> serverPort
			>> clientPort
			>> connectionTimeOut
			>> maxConnections
			>> occasionalPing
			>> shutdownTime
			>> unreliableTimeout;

	file.close();
}

void NetworkConnectionInfo::saveToFile( const std::string &filename )
{	 
	std::ofstream file;
	file.open( filename.c_str(), std::ios::out );
	if( !file.is_open() )
	{
		ATMOS_THROW_RT("failed to open file: " << filename);
	}

	file	<< serverIP << std::endl
			<< serverPort << std::endl
			<< clientPort << std::endl
			<< connectionTimeOut << std::endl
			<< maxConnections << std::endl
			<< occasionalPing << std::endl
			<< shutdownTime << std::endl
			<< unreliableTimeout << std::endl;

	file.close();
}